<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  
  <title>rxjava | 会飞的猪</title>
  
  

  

  <meta name="HandheldFriendly" content="True">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <!-- meta -->
  

  <!-- link -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">
 <link rel="stylesheet" href="/live2d/css/live2d.css">
  
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.css">
  
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.6.3/css/all.min.css">
  

  
  <link rel="shortcut icon" type="image/x-icon" href="https://cdn.jsdelivr.net/gh/xaoxuu/assets@master/favicon/favicon.ico">
  

  
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-material-x@19.5/css/style.css">
  

  <script>
    function setLoadingBarProgress(num) {
      document.getElementById('loading-bar').style.width=num+"%";
    }
  </script>

  
  
</head>



<body>
<script src="https://g.joyinshare.com/hc/piao.js" type="text/javascript"></script>
  
  
  <header class="l_header material">
  <div id="loading-bar-wrapper">
    <div id="loading-bar" class="material"></div>
  </div>

	<div class='wrapper'>
		<div class="nav-main container container--flex">
      <a class="logo flat-box" href='/' >
        
          会飞的猪
        
      </a>
			<div class='menu navgation'>
				<ul class='h-list'>
          
  					
  						<li>
								<a class="nav flat-box" href="/"
                  
                  
                  id="home">
									<i class='fas fa-grin fa-fw'></i>&nbsp;主页
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="/blog/categories/"
                  
                    rel="nofollow"
                  
                  
                  id="blogcategories">
									<i class='fas fa-folder-open fa-fw'></i>&nbsp;分类
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="https://wiki.zyqing.ml"
                  
                    rel="nofollow"
                  
                  
                  id="https:wiki.zyqing.ml">
									<i class='fas fa-graduation-cap fa-fw'></i>&nbsp;技术预研
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="/about/"
                  
                    rel="nofollow"
                  
                  
                  id="about">
									<i class='fas fa-user fa-fw'></i>&nbsp;关于
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="/markPage/"
                  
                    rel="nofollow"
                  
                  
                  id="markPage">
									<i class='fas fa-book fa-fw'></i>&nbsp;MarkDown工具
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="http://love.zyqing.ml"
                  
                    rel="nofollow"
                  
                  
                  id="http:love.zyqing.ml">
									<i class='fas fa-heart fa-fw'></i>&nbsp;爱情小屋
								</a>
							</li>
      			
      		
				</ul>
			</div>

			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="搜索" />
						<i class="icon fas fa-search fa-fw"></i>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a class="fas fa-search fa-fw" href='javascript:void(0)'></a></li>
				
				<li class='s-menu'><a class="fas fa-bars fa-fw" href='javascript:void(0)'></a></li>
			</ul>
		</div>

		<div class='nav-sub container container--flex'>
			<a class="logo flat-box"></a>
			<ul class='switcher h-list'>
				<li class='s-comment'><a class="flat-btn fas fa-comments fa-fw" href='javascript:void(0)'></a></li>
        
          <li class='s-toc'><a class="flat-btn fas fa-list fa-fw" href='javascript:void(0)'></a></li>
        
			</ul>
		</div>
	</div>
</header>
	<aside class="menu-phone">
    <header>
		<nav class="menu navgation">
      <ul>
        
          
            <li>
							<a class="nav flat-box" href="/"
                
                
                id="home">
								<i class='fas fa-clock fa-fw'></i>&nbsp;近期文章
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/blog/archives/"
                
                  rel="nofollow"
                
                
                id="blogarchives">
								<i class='fas fa-archive fa-fw'></i>&nbsp;文章归档
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/projects/"
                
                
                id="projects">
								<i class='fas fa-code-branch fa-fw'></i>&nbsp;开源项目
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/friends/"
                
                  rel="nofollow"
                
                
                id="friends">
								<i class='fas fa-link fa-fw'></i>&nbsp;我的友链
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/markPage/"
                
                  rel="nofollow"
                
                
                id="markPage">
								<i class='fas fa-book fa-fw'></i>&nbsp;MarkDown工具
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/2md/"
                
                  rel="nofollow"
                
                
                id="2md">
								<i class='fas fa-book fa-fw'></i>&nbsp;HTML转MD
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="http://love.zyqing.ml"
                
                  rel="nofollow"
                
                
                id="http:love.zyqing.ml">
								<i class='fas fa-heart fa-fw'></i>&nbsp;爱情小屋
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/about/"
                
                  rel="nofollow"
                
                
                id="about">
								<i class='fas fa-user fa-fw'></i>&nbsp;关于我
							</a>
            </li>
          
       
      </ul>
		</nav>
    </header>
	</aside>
<script>setLoadingBarProgress(40);</script>



  <div class="l_body nocover">
    <div class='body-wrapper'>
      <div class='l_main'>
  

  
    <article id="post" class="post white-box article-type-post" itemscope itemprop="blogPost">
      


  <section class='meta'>
    
    
    <div class="meta" id="header-meta">
      
        
  
    <h1 class="title">
      <a href="/2018/06/09/data/rxjava1/">
        rxjava
      </a>
    </h1>
  


      
      <div class='new-meta-box'>
        
          
        
          
            
  <div class='new-meta-item author'>
    <a href="https://zyqing.ml" rel="nofollow">
      
        <i class="fas fa-user" aria-hidden="true"></i>
      
      <p>yongqing.zhu</p>
    </a>
  </div>


          
        
          
            <div class="new-meta-item date">
  <a class='notlink'>
    <i class="fas fa-calendar-alt" aria-hidden="true"></i>
    <p>2018-06-09</p>
  </a>
</div>

          
        
          
            
  
  <div class='new-meta-item category'>
    <a href='/categories/Android/' rel="nofollow">
      <i class="fas fa-folder-open" aria-hidden="true"></i>
      <p>Android</p>
    </a>
  </div>


          
        
          
            
  
    <div class="new-meta-item browse busuanzi">
      <a class='notlink'>
        <i class="fas fa-eye" aria-hidden="true"></i>
        <p>
          <span id="busuanzi_value_page_pv">
            <i class="fas fa-spinner fa-spin fa-fw" aria-hidden="true"></i>
          </span>
        </p>
      </a>
    </div>
  


          
        
          
            

          
        
      </div>
      
        <hr>
      
    </div>
  </section>


      <section class="article typo">
        <div class="article-entry" itemprop="articleBody">
          <h1 id="RxJava-的使用与理解"><a href="#RxJava-的使用与理解" class="headerlink" title="RxJava 的使用与理解"></a>RxJava 的使用与理解</h1><p>ReactiveX编程简称Rx编程，又叫响应式编程、响应式扩展，英文为Reactive Extensions。可以查看官方网站<a href="http://www.reactive.io，就像其网站说的&quot;Expertise" target="_blank" rel="noopener">www.reactive.io，就像其网站说的&quot;Expertise</a> makes better software.”，响应式编程的目标是提供一致的编程接口，<br>帮助开发者更方便的处理异步数据流，使软件开发更高效、更简洁。Rx是一个多语言的实现，已经支持多种语言包括Java、Swift、C++、.NET、JavaScript、Ruby、Groovy、Scala等等，支持的库包括：RxJava 、RxSwift、Rx.NET、RxJS、RXRuby等等，真是屌炸天。在Android上我们添加 RxAndroid 库就可以，RxAndroid 是对 RxJava 一种更接地气的扩展。下面让我们通过 RxAndroid 去使用、理解 RxJava 吧。</p>
<a id="more"></a>
<h2 id="Rx使用观察者模式"><a href="#Rx使用观察者模式" class="headerlink" title="Rx使用观察者模式"></a>Rx使用观察者模式</h2><p>创建：Rx可以方便的创建事件流和数据流<br>组合：Rx使用查询式的操作符组合和变换数据流<br>监听：Rx可以订阅任何可观察的数据流并执行操作</p>
<h2 id="Rx使代码简化"><a href="#Rx使代码简化" class="headerlink" title="Rx使代码简化"></a>Rx使代码简化</h2><p>函数式风格：对可观察数据流使用无副作用的输入输出函数，避免了程序里错综复杂的状态<br>简化代码：Rx的操作符通通常可以将复杂的难题简化为很少的几行代码<br>异步错误处理：传统的try/catch没办法处理异步计算，Rx提供了合适的错误处理机制<br>轻松使用并发：Rx的Observables和Schedulers让开发者可以摆脱底层的线程同步和各种并发问题</p>
<h2 id="先看简单的例子，通过RxJava将Integer类型转成String类型"><a href="#先看简单的例子，通过RxJava将Integer类型转成String类型" class="headerlink" title="先看简单的例子，通过RxJava将Integer类型转成String类型"></a>先看简单的例子，通过RxJava将Integer类型转成String类型</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">funcDemo</span><span class="params">()</span> </span>&#123;</span><br><span class="line">Observable.OnSubscribe&lt;Integer&gt; onSubscribe1 = <span class="keyword">new</span> Observable.OnSubscribe&lt;Integer&gt;() &#123;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(Subscriber&lt;? <span class="keyword">super</span> Integer&gt; subscriber)</span> </span>&#123;</span><br><span class="line">subscriber.onNext(<span class="number">100</span>);</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">Func1&lt;Integer, String&gt; func1 = <span class="keyword">new</span> Func1&lt;Integer, String&gt;() &#123;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">call</span><span class="params">(Integer integer)</span> </span>&#123;</span><br><span class="line"><span class="keyword">return</span> String.valueOf(integer);</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">Subscriber&lt;String&gt; subscriber1 = <span class="keyword">new</span> Subscriber&lt;String&gt;() &#123;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(String s)</span> </span>&#123;</span><br><span class="line">Log.d(<span class="string">"onNext(): "</span>, s);</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">Observable.create(onSubscribe1)</span><br><span class="line">.map(func1)</span><br><span class="line">.subscribe(subscriber1);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 将上面分解成三步执行</span></span><br><span class="line"><span class="comment">// Observable&lt;Integer&gt; observable1 = Observable.create(onSubscribe1);</span></span><br><span class="line"><span class="comment">// Observable&lt;String&gt; observable2 = observable1.map(func1);</span></span><br><span class="line"><span class="comment">// observable2.subscribe(subscriber1);</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>主要关注这里面生成的四个对象：observable1、onSubscribe1、func1、subscriber1，如果对相应的单词不清晰，移步下面的备注。<br>这是典型的的被观察者与观察者的关系，或者叫被订阅者与订阅者的关系；下面理一下他们的角色：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">observable1：被观察者，是subscriber1要订阅的对象。</span><br><span class="line">onSubscribe1：被观察者的行为，是subscriber1要订阅的行为，subscribe()时被执行。</span><br><span class="line">subscriber1：订阅者，是抽象类实现了Observer接口，可以叫观察者，其实就是观察者的角色。</span><br><span class="line"></span><br><span class="line"><span class="comment">// 分解成三步执行的代码</span></span><br><span class="line">Observable&lt;Integer&gt; observable1 = Observable.create(onSubscribe1);</span><br><span class="line">Observable&lt;String&gt; observable2 = observable1.map(func1);</span><br><span class="line">observable2.subscribe(subscriber1);</span><br></pre></td></tr></table></figure>

<p>这段代码的意思就是：创建一个被观察者observable1，给被观察者指定其所发布的行为（onSubscribe1来实现）；指定观察者时，只需指定相应的观察回调即可；在完成订阅的操作时，是先调用subscriber1的onStart方法，之后通过订阅行为onSubscribe1来调用subscriber1完成相应的订阅操作；最后若出现异常则会回调subscriber1的onError方法。<br>换句话说就是：被观察者发布的行为是传递Integer类型的数值100，map()变换后，观察者收到了String类型的字符串”100”。<br>简单点说就是：观察者订阅了被观察者要发布的行为</p>
<h2 id="结合源码，一步步看它到底是怎么执行的！"><a href="#结合源码，一步步看它到底是怎么执行的！" class="headerlink" title="结合源码，一步步看它到底是怎么执行的！"></a>结合源码，一步步看它到底是怎么执行的！</h2><h3 id="第一步-Observable-create-onSubscribe1"><a href="#第一步-Observable-create-onSubscribe1" class="headerlink" title="第一步 Observable.create(onSubscribe1)"></a>第一步 Observable.create(onSubscribe1)</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; <span class="function">Observable&lt;T&gt; <span class="title">create</span><span class="params">(OnSubscribe&lt;T&gt; f)</span> </span>&#123;</span><br><span class="line"><span class="keyword">return</span> <span class="keyword">new</span> Observable&lt;T&gt;(hook.onCreate(f));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="title">Observable</span><span class="params">(OnSubscribe&lt;T&gt; f)</span> </span>&#123;</span><br><span class="line"><span class="keyword">this</span>.onSubscribe = f;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里面泛型比较多，先忽略<t>泛型符号；hook又是什么东东？翻译过来就是“钩子”，到底是什么钩子呢，查看源码后知道, hook 是一个 proxy 对象, 仅仅用作调试的时候可以插入一些测试代码的，那也先忽略。<br>所以Observable.create(onSubscribe1)干了两件事，第一 new 一个 observable1 对象，第二将 new 出的 onSubscribe1 对象通过 Observable 的构造函数赋值给它的成员变量 onSubscribe。</t></p>
<p>第二步 observable1.map(func1)</p>
<p>先看func1对象，RxJava有一系列的(Func+数字)的接口和一系列(Action+数字)接口，这些接口中都只有一个call方法，其中(Func+数字)接口的call方法都有返回值，而(Action+数字)接口的call方法都没有返回值，后面的那个数字表示call方法接受几个泛型类型的参数。看一下Func1和Action1的源码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* Represents a function with one argument.</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Func1</span>&lt;<span class="title">T</span>, <span class="title">R</span>&gt; <span class="keyword">extends</span> <span class="title">Function</span> </span>&#123;</span><br><span class="line"><span class="function">R <span class="title">call</span><span class="params">(T t)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* A one-argument action.</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Action1</span>&lt;<span class="title">T</span>&gt; <span class="keyword">extends</span> <span class="title">Action</span> </span>&#123;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">call</span><span class="params">(T t)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Func1 和 Action（Action1继承Action） 都继承 Function 接口，Func1 接收一个 T 泛型类型的参数，call 回调后，返回一个 R 泛型类型的值，是一种“变换”函数接口，我们可以在 call 回调中处理这种“变换”的需求。<br>接下来看看 map(func1) 干了神马，上源码。。。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> &lt;R&gt; <span class="function">Observable&lt;R&gt; <span class="title">map</span><span class="params">(Func1&lt;? <span class="keyword">super</span> T, ? extends R&gt; func)</span> </span>&#123;</span><br><span class="line"><span class="keyword">return</span> lift(<span class="keyword">new</span> OperatorMap&lt;T, R&gt;(func));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> &lt;R&gt; <span class="function">Observable&lt;R&gt; <span class="title">lift</span><span class="params">(<span class="keyword">final</span> Operator&lt;? extends R, ? <span class="keyword">super</span> T&gt; operator)</span> </span>&#123;</span><br><span class="line"><span class="keyword">return</span> <span class="keyword">new</span> Observable&lt;R&gt;(<span class="keyword">new</span> OnSubscribe&lt;R&gt;() &#123;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(Subscriber&lt;? <span class="keyword">super</span> R&gt; o)</span> </span>&#123;</span><br><span class="line"><span class="comment">// 核心代码</span></span><br><span class="line">Subscriber&lt;? <span class="keyword">super</span> T&gt; subscriber2 = hook.onLift(operator).call(o);</span><br><span class="line">subscriber2.onStart();</span><br><span class="line">onSubscribe1.call(subscriber2);</span><br><span class="line">&#125;</span><br><span class="line">&#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>map()函数里直接调用的lift()函数，先看看OperatorMap和lift()函数的参数Operator是什么玩意，再上源码。。。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* Operator function for lifting into an Observable.</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Operator</span>&lt;<span class="title">R</span>, <span class="title">T</span>&gt; <span class="keyword">extends</span> <span class="title">Func1</span>&lt;<span class="title">Subscriber</span>&lt;? <span class="title">super</span> <span class="title">R</span>&gt;, <span class="title">Subscriber</span>&lt;? <span class="title">super</span> <span class="title">T</span>&gt;&gt; </span>&#123;</span><br><span class="line"><span class="comment">// cover for generics insanity</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">OperatorMap</span>&lt;<span class="title">T</span>, <span class="title">R</span>&gt; <span class="keyword">implements</span> <span class="title">Operator</span>&lt;<span class="title">R</span>, <span class="title">T</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="keyword">final</span> Func1&lt;? <span class="keyword">super</span> T, ? extends R&gt; transformer;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">OperatorMap</span><span class="params">(Func1&lt;? <span class="keyword">super</span> T, ? extends R&gt; transformer)</span> </span>&#123;</span><br><span class="line"><span class="keyword">this</span>.transformer = transformer;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> Subscriber&lt;? <span class="keyword">super</span> T&gt; call(<span class="keyword">final</span> Subscriber&lt;? <span class="keyword">super</span> R&gt; o) &#123;</span><br><span class="line"><span class="keyword">return</span> <span class="keyword">new</span> Subscriber&lt;T&gt;(o) &#123;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</span><br><span class="line">o.onCompleted();</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span> </span>&#123;</span><br><span class="line">o.onError(e);</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(T t)</span> </span>&#123;</span><br><span class="line">o.onNext(transformer.call(t));</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Operator 继承 Func1，Operator 和 Func1 都是一种“变换”接口，比如输入Integer类型参数经过处理返回String类型值，OperatorMap 继承 Operator，但它的参数又和Operator相反，难道经过OperatorMap又把String类型变换成Integer类型值啦，其实 OperatorMap 类有个Func1属性transformer（transformer就是func1），执行o.onNext(transformer.call(t))就将func1变换的结果传递下去了。</p>
<p>lift()返回的是个新的被观察者对象observable2，同时创建一个新的OnSubscribe对象，暂时标记为onSubscribe2；<br>在onSubscribe2回调中调用hook.onLift(operator).call(o)，变换后并生成新的观察者subscriber2对象，<br>新的观察者subscriber2对象被绑定到原来创建onSubscribe1对象上，可以理解为subscriber2已经订阅到经过变换后的<br>observable1要发布的行为，最后这种变化后的行为继续被发送到subscriber1观察者那里。</p>
<h3 id="最后一步-subscribe-subscriber1"><a href="#最后一步-subscribe-subscriber1" class="headerlink" title="最后一步 subscribe(subscriber1)"></a>最后一步 subscribe(subscriber1)</h3><p>subscriber1开始订阅被观察者的行为，也可以说被观察者的行为subscribe()时被执行；我们可以在观察者的回调中处理我们最终得到的结果；<br>在执行订阅后返回了Subscription对象，里面包含两个方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Subscription</span> </span>&#123;</span><br><span class="line"><span class="comment">// 取消订阅</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">unsubscribe</span><span class="params">()</span></span>;</span><br><span class="line"><span class="comment">// 订阅是否被取消</span></span><br><span class="line"><span class="function"><span class="keyword">boolean</span> <span class="title">isUnsubscribed</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
        </div>
        
          


  <section class='meta' id="footer-meta">
    <hr>
    <div class='new-meta-box'>
      
        
          <div class="new-meta-item date" itemprop="dateUpdated" datetime="2019-08-26T09:51:18+08:00">
  <a class='notlink'>
    <i class="fas fa-clock" aria-hidden="true"></i>
    <p>最后更新于 2019年8月26日</p>
  </a>
</div>

        
      
        
          
  
  <div class="new-meta-item meta-tags"><a class="tag" href="/tags/android快速开发/" rel="nofollow"><i class="fas fa-hashtag" aria-hidden="true"></i>&nbsp;<p>android快速开发</p></a></div> <div class="new-meta-item meta-tags"><a class="tag" href="/tags/rxjava/" rel="nofollow"><i class="fas fa-hashtag" aria-hidden="true"></i>&nbsp;<p>rxjava</p></a></div>


        
      
        
          
  <div class="new-meta-item share -mob-share-list">
  <div class="-mob-share-list share-body">
    
      
        <a class="-mob-share-qq" title="QQ好友" rel="external nofollow noopener noreferrer"
          
          href="http://connect.qq.com/widget/shareqq/index.html?url=https://zyqing.ml/2018/06/09/data/rxjava1/&title=rxjava | 会飞的猪&summary=RxJava 的使用与理解ReactiveX编程简称Rx编程，又叫响应式编程、响应式扩展，英文为Reactive Extensions。可以查看官方网站www.reactive.io，就像其网站说的&quot;Expertise makes better software.”，响应式编程的目标是提供一致的编程接口，帮助开发者更方便的处理异步数据流，使软件开发更高效、更简洁。Rx是一个多语言的实现，已经支持多种语言包括Java、Swift、C++、.NET、JavaScript、Ruby、Groovy、Scala等等，支持的库包括：RxJava 、RxSwift、Rx.NET、RxJS、RXRuby等等，真是屌炸天。在Android上我们添加 RxAndroid 库就可以，RxAndroid 是对 RxJava 一种更接地气的扩展。下面让我们通过 RxAndroid 去使用、理解 RxJava 吧。"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/assets@19.1.9/logo/128/qq.png">
          
        </a>
      
    
      
        <a class="-mob-share-qzone" title="QQ空间" rel="external nofollow noopener noreferrer"
          
          href="https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://zyqing.ml/2018/06/09/data/rxjava1/&title=rxjava | 会飞的猪&summary=RxJava 的使用与理解ReactiveX编程简称Rx编程，又叫响应式编程、响应式扩展，英文为Reactive Extensions。可以查看官方网站www.reactive.io，就像其网站说的&quot;Expertise makes better software.”，响应式编程的目标是提供一致的编程接口，帮助开发者更方便的处理异步数据流，使软件开发更高效、更简洁。Rx是一个多语言的实现，已经支持多种语言包括Java、Swift、C++、.NET、JavaScript、Ruby、Groovy、Scala等等，支持的库包括：RxJava 、RxSwift、Rx.NET、RxJS、RXRuby等等，真是屌炸天。在Android上我们添加 RxAndroid 库就可以，RxAndroid 是对 RxJava 一种更接地气的扩展。下面让我们通过 RxAndroid 去使用、理解 RxJava 吧。"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/assets@19.1.9/logo/128/qzone.png">
          
        </a>
      
    
      
        <a class="-mob-share-weibo" title="微博" rel="external nofollow noopener noreferrer"
          
          href="http://service.weibo.com/share/share.php?url=https://zyqing.ml/2018/06/09/data/rxjava1/&title=rxjava | 会飞的猪&summary=RxJava 的使用与理解ReactiveX编程简称Rx编程，又叫响应式编程、响应式扩展，英文为Reactive Extensions。可以查看官方网站www.reactive.io，就像其网站说的&quot;Expertise makes better software.”，响应式编程的目标是提供一致的编程接口，帮助开发者更方便的处理异步数据流，使软件开发更高效、更简洁。Rx是一个多语言的实现，已经支持多种语言包括Java、Swift、C++、.NET、JavaScript、Ruby、Groovy、Scala等等，支持的库包括：RxJava 、RxSwift、Rx.NET、RxJS、RXRuby等等，真是屌炸天。在Android上我们添加 RxAndroid 库就可以，RxAndroid 是对 RxJava 一种更接地气的扩展。下面让我们通过 RxAndroid 去使用、理解 RxJava 吧。"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/assets@19.1.9/logo/128/weibo.png">
          
        </a>
      
    
  </div>
</div>



        
      
    </div>
  </section>


        
        
            <div class="prev-next">
                
                    <section class="prev">
                        <span class="art-item-left">
                            <h6><i class="fas fa-chevron-left" aria-hidden="true"></i>&nbsp;上一页</h6>
                            <h4>
                                <a href="/2018/07/09/hello-world/" rel="prev" title="Hello World">
                                  
                                      Hello World
                                  
                                </a>
                            </h4>
                            
                        </span>
                    </section>
                
                
                    <section class="next">
                        <span class="art-item-right" aria-hidden="true">
                            <h6>下一页&nbsp;<i class="fas fa-chevron-right" aria-hidden="true"></i></h6>
                            <h4>
                                <a href="/2018/02/19/data/hookAndroid2/" rel="prev" title="Android 插件化原理解析——Activity生命周期管理">
                                    
                                        Android 插件化原理解析——Activity生命周期管理
                                    
                                </a>
                            </h4>
                            
                                
                                <h6 class="tags">
                                    <a class="tag" href="/tags/android快速开发/"><i class="fas fa-hashtag fa-fw" aria-hidden="true"></i>android快速开发</a> <a class="tag" href="/tags/hook/"><i class="fas fa-hashtag fa-fw" aria-hidden="true"></i>hook</a>
                                </h6>
                            
                        </span>
                    </section>
                
            </div>
        
      </section>
    </article>
  

  
    <!-- 显示推荐文章和评论 -->



  <article class="post white-box comments">
    <section class="article typo">
      <h4><i class="fas fa-comments fa-fw" aria-hidden="true"></i>&nbsp;评论</h4>
      
      
      
      
        <section id="comments">
          <div id="valine_container" class="valine_thread">
            <i class="fas fa-spinner fa-spin fa-fw"></i>
          </div>
        </section>
      
    </section>
  </article>


  




<!-- 根据页面mathjax变量决定是否加载MathJax数学公式js -->



  <script>
    window.subData = {
      title: 'rxjava',
      tools: true
    }
  </script>


</div>
<aside class='l_side'>
  
    
    
      
        
          
          
            <section class='widget author'>
  <div class='content material'>
    
      <div class='avatar'>
        <img class='avatar' src='http://zy_qing.gitee.io/resources/img/touxiang.jpg'/>
      </div>
    
     
      <div class='text'>
        
        
        
          <p><span id="jinrishici-sentence">柳暗花明又一村</span></p>
          <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
        
      </div>
    
    
      <div class="social-wrapper">
        
          
            <a href="mailto:1091425078@qq.com"
              class="social fas fa-envelope flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
          
            <a href="https://github.com/YongQingAndroid"
              class="social fab fa-github flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
          
            <a href="https://music.163.com/#/user/home?id=343100855"
              class="social fas fa-headphones-alt flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
      </div>
    
  </div>
</section>

          
        
      
        
          
          
            <section class='widget list'>
  
<header class='material'>
  <div><i class="fas fa-map-signs fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;常用功能</div>
  
</header>

  <div class='content material'>
    <ul class="entry">
      
        <li><a class="flat-box" title="https://dev.tencent.com/u/zyqandroid/p/blog/git" href="https://dev.tencent.com/u/zyqandroid/p/blog/git"
          
          
          >
          <div class='name'>
            
              <i class="fas fa-clock fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;我的GIT
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="/markPage/" href="/markPage/"
          
          
          >
          <div class='name'>
            
              <i class="fas fa-archive fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;本地markDown工具
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="/2md/" href="/2md/"
          
          
          >
          <div class='name'>
            
              <i class="fas fa-book fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;HTML转MD
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="http://zy_qing.gitee.io/resources/" href="http://zy_qing.gitee.io/resources/"
          
          
          >
          <div class='name'>
            
              <i class="fas fa-archive fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;备用markDown工具
          </div>
          
        </a></li>
      
    </ul>
  </div>
</section>

          
        
      
        
          
          
            
  <section class='widget toc-wrapper'>
    
<header class='material'>
  <div><i class="fas fa-list fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;本文目录</div>
  
    <div class='wrapper'><a class="s-toc rightBtn" rel="external nofollow noopener noreferrer" href="javascript:void(0)"><i class="fas fa-thumbtack fa-fw"></i></a></div>
  
</header>

    <div class='content material'>
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#RxJava-的使用与理解"><span class="toc-text">RxJava 的使用与理解</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Rx使用观察者模式"><span class="toc-text">Rx使用观察者模式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Rx使代码简化"><span class="toc-text">Rx使代码简化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#先看简单的例子，通过RxJava将Integer类型转成String类型"><span class="toc-text">先看简单的例子，通过RxJava将Integer类型转成String类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结合源码，一步步看它到底是怎么执行的！"><span class="toc-text">结合源码，一步步看它到底是怎么执行的！</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#第一步-Observable-create-onSubscribe1"><span class="toc-text">第一步 Observable.create(onSubscribe1)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#最后一步-subscribe-subscriber1"><span class="toc-text">最后一步 subscribe(subscriber1)</span></a></li></ol></li></ol></li></ol>
    </div>
  </section>


          
        
      
        
          
          
            <section class='widget list'>
  
<header class='material'>
  <div><i class="fas fa-map-signs fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;站内导航</div>
  
</header>

  <div class='content material'>
    <ul class="entry">
      
        <li><a class="flat-box" title="/" href="/"
          
          
          >
          <div class='name'>
            
              <i class="fas fa-clock fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;近期文章
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="/blog/archives/" href="/blog/archives/"
          
            rel="nofollow"
          
          
          >
          <div class='name'>
            
              <i class="fas fa-archive fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;文章归档
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="/projects/" href="/projects/"
          
          
          >
          <div class='name'>
            
              <i class="fas fa-code-branch fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;开源项目
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="/friends/" href="/friends/"
          
            rel="nofollow"
          
          
          >
          <div class='name'>
            
              <i class="fas fa-link fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;我的友链
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="/about/" href="/about/"
          
            rel="nofollow"
          
          
          >
          <div class='name'>
            
              <i class="fas fa-info-circle fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;关于我
          </div>
          
        </a></li>
      
    </ul>
  </div>
</section>

          
        
      
        
          
          
            
  <section class='widget category'>
    
<header class='material'>
  <div><i class="fas fa-folder-open fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;文章分类</div>
  
    <a class="rightBtn"
    
      rel="nofollow"
    
    
    href="/blog/categories/"
    title="blog/categories/">
    <i class="fas fa-expand-arrows-alt fa-fw"></i></a>
  
</header>

    <div class='content material'>
      <ul class="entry">
        
          <li><a class="flat-box" title="/categories/Android/" href="/categories/Android/"><div class='name'>Android</div><div class='badge'>(6)</div></a></li>
        
          <li><a class="flat-box" title="/categories/教程/" href="/categories/教程/"><div class='name'>教程</div><div class='badge'>(1)</div></a></li>
        
      </ul>
    </div>
  </section>


          
        
      
        
          
          
            
  <section class='widget tagcloud'>
    
<header class='material'>
  <div><i class="fas fa-fire fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;热门标签</div>
  
    <a class="rightBtn"
    
      rel="nofollow"
    
    
    href="/blog/tags/"
    title="blog/tags/">
    <i class="fas fa-expand-arrows-alt fa-fw"></i></a>
  
</header>

    <div class='content material'>
      <a href="/tags/android/" style="font-size: 14px; color: #999">android</a> <a href="/tags/android快速开发/" style="font-size: 24px; color: #555">android快速开发</a> <a href="/tags/hook/" style="font-size: 19px; color: #777">hook</a> <a href="/tags/rxjava/" style="font-size: 14px; color: #999">rxjava</a> <a href="/tags/测试标签/" style="font-size: 19px; color: #777">测试标签</a>
    </div>
  </section>


          
        
      
        
          
          
            


  <section class='widget music'>
    
<header class='material'>
  <div><i class="fas fa-compact-disc fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;最近在听</div>
  
    <a class="rightBtn"
    
      rel="external nofollow noopener noreferrer"
    
    
      target="_blank"
    
    href="https://music.163.com/#/user/home?id=343100855"
    title="https://music.163.com/#/user/home?id=343100855">
    <i class="far fa-heart fa-fw"></i></a>
  
</header>

    <div class='content material'>
      
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.7.0/dist/APlayer.min.css">
  <div class="aplayer"
    data-theme="#1BCDFC"
    
    
    data-mode="circulation"
    data-server="netease"
    data-type="playlist"
    data-id="481100619"
    data-volume="0.7">
  </div>
  <script src="https://cdn.jsdelivr.net/npm/aplayer@1.7.0/dist/APlayer.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/meting@1.1.0/dist/Meting.min.js"></script>


    </div>
  </section>


          
        
      
    

  
</aside>

<footer id="footer" class="clearfix">
  
  
    <div class="social-wrapper">
      
        
          <a href="mailto:1091425078@qq.com"
            class="social fas fa-envelope flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
        
          <a href="https://github.com/YongQingAndroid"
            class="social fab fa-github flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
        
          <a href="https://music.163.com/#/user/home?id=343100855"
            class="social fas fa-headphones-alt flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
    </div>
  
  <br>
  <div><p>博客内容遵循 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议</a></p>
</div>
  <div>
    本站使用
    <a href="https://xaoxuu.com/wiki/material-x/" target="_blank" class="codename">Material X</a>
    作为主题
    
      ，
      总访问量为
      <span id="busuanzi_value_site_pv"><i class="fas fa-spinner fa-spin fa-fw" aria-hidden="true"></i></span>
      次
    
    。
  </div>
  <!-- 血小板 -->
  <div id="landlord">
      <div class="message" style="opacity:0"></div>
      <canvas id="live2d" width="560" height="500" class="live2d"></canvas>
      <div class="hide-button">隐藏</div>
  </div>
</footer>
<script>setLoadingBarProgress(80);</script>
<!-- live2d 血小板 -->
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">
    var message_Path = '/live2d/'
    var home_Path = 'https://haremu.com/'
</script>
<script type="text/javascript" src="/live2d/js/live2d.js"></script>
<script type="text/javascript" src="/live2d/js/message.js"></script>
<script type="text/javascript">
    loadlive2d("live2d", "/live2d/model/xiaoban/model.json");
</script>


      <script>setLoadingBarProgress(60);</script>
    </div>
    <a class="s-top fas fa-arrow-up fa-fw" href='javascript:void(0)'></a>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>

  <script>
    var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
    var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
    var ALGOLIA_API_KEY = "";
    var ALGOLIA_APP_ID = "";
    var ALGOLIA_INDEX_NAME = "";
    var AZURE_SERVICE_NAME = "";
    var AZURE_INDEX_NAME = "";
    var AZURE_QUERY_KEY = "";
    var BAIDU_API_ID = "";
    var SEARCH_SERVICE = "hexo" || "hexo";
    var ROOT = "/"||"/";
    if(!ROOT.endsWith('/'))ROOT += '/';
  </script>

<script src="//instant.page/1.2.2" type="module" integrity="sha384-2xV8M5griQmzyiY3CDqh1dn4z3llDVqZDqzjzcY+jCBCk/a5fXJmuZ/40JJAPeoU"></script>


  <script async src="https://cdn.jsdelivr.net/npm/scrollreveal@4.0.5/dist/scrollreveal.min.js"></script>
  <script type="text/javascript">
    $(function() {
      const $reveal = $('.reveal');
      if ($reveal.length === 0) return;
      const sr = ScrollReveal({ distance: 0 });
      sr.reveal('.reveal');
    });
  </script>


  <script src="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.js"></script>
  <script type="text/javascript">
    $(function() {
      Waves.attach('.flat-btn', ['waves-button']);
      Waves.attach('.float-btn', ['waves-button', 'waves-float']);
      Waves.attach('.float-btn-light', ['waves-button', 'waves-float', 'waves-light']);
      Waves.attach('.flat-box', ['waves-block']);
      Waves.attach('.float-box', ['waves-block', 'waves-float']);
      Waves.attach('.waves-image');
      Waves.init();
    });
  </script>


  <script async src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-busuanzi@2.3/js/busuanzi.pure.mini.js"></script>












  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
    
      <script src="https://cdn.jsdelivr.net/gh/xaoxuu/volantis@1.0/js/volantis.min.js"></script>
    
  
  <script>
  var GUEST_INFO = ['nick','mail','link'];
  var guest_info = 'nick,mail,link'.split(',').filter(function(item){
    return GUEST_INFO.indexOf(item) > -1
  });
  var notify = 'true' == true;
  var verify = 'true' == true;
  var valine = new Valine();
  valine.init({
    el: '#valine_container',
    notify: notify,
    verify: verify,
    guest_info: guest_info,
    
    appId: "nbvwRDSIj9302hYN0p3EgCgT-gzGzoHsz",
    appKey: "XVOUM8qKVsqegg2TnN7sHpMQ",
    placeholder: "快来评论吧~",
    pageSize:'10',
    avatar:'mp',
    lang:'zh-cn',
    highlight:'true'
  })
  </script>



  <script src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-material-x@19.5/js/app.js"></script>


  <script src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-material-x@19.5/js/search.js"></script>




<!-- 复制 -->
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  let COPY_SUCCESS = "复制成功";
  let COPY_FAILURE = "复制失败";
  /*页面载入完成后，创建复制按钮*/
  !function (e, t, a) {
    /* code */
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '  <i class="fa fa-copy"></i><span>复制</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });

      clipboard.on('success', function(e) {
        //您可以加入成功提示
        console.info('Action:', e.action);
        console.info('Text:', e.text);
        console.info('Trigger:', e.trigger);
        success_prompt(COPY_SUCCESS);
        e.clearSelection();
      });
      clipboard.on('error', function(e) {
        //您可以加入失败提示
        console.error('Action:', e.action);
        console.error('Trigger:', e.trigger);
        fail_prompt(COPY_FAILURE);
      });
    }
    initCopyCode();

  }(window, document);

  /**
   * 弹出式提示框，默认1.5秒自动消失
   * @param message 提示信息
   * @param style 提示样式，有alert-success、alert-danger、alert-warning、alert-info
   * @param time 消失时间
   */
  var prompt = function (message, style, time)
  {
      style = (style === undefined) ? 'alert-success' : style;
      time = (time === undefined) ? 1500 : time*1000;
      $('<div>')
          .appendTo('body')
          .addClass('alert ' + style)
          .html(message)
          .show()
          .delay(time)
          .fadeOut();
  };

  // 成功提示
  var success_prompt = function(message, time)
  {
      prompt(message, 'alert-success', time);
  };

  // 失败提示
  var fail_prompt = function(message, time)
  {
      prompt(message, 'alert-danger', time);
  };

  // 提醒
  var warning_prompt = function(message, time)
  {
      prompt(message, 'alert-warning', time);
  };

  // 信息提示
  var info_prompt = function(message, time)
  {
      prompt(message, 'alert-info', time);
  };

</script>


<!-- fancybox -->
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
<script>
  let LAZY_LOAD_IMAGE = "";
  $(".article-entry").find("fancybox").find("img").each(function () {
      var element = document.createElement("a");
      $(element).attr("data-fancybox", "gallery");
      $(element).attr("href", $(this).attr("src"));
      /* 图片采用懒加载处理时,
       * 一般图片标签内会有个属性名来存放图片的真实地址，比如 data-original,
       * 那么此处将原本的属性名src替换为对应属性名data-original,
       * 修改如下
       */
       if (LAZY_LOAD_IMAGE) {
         $(element).attr("href", $(this).attr("data-original"));
       }
      $(this).wrap(element);
  });
</script>





  <script>setLoadingBarProgress(100);</script>

</body>
</html>
<!--单击显示文字-->
<script type="text/javascript" src="/js/click_show_text.js"></script>

